package cn.tedu.crm.notice.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.tedu.crm.user.mapper.UserMapper;
import cn.tedu.crm.notice.mapper.NoticeMapper;
import cn.tedu.crm.notice.pojo.dto.NoticeQuery;
import cn.tedu.crm.notice.pojo.dto.NoticeSaveParam;
import cn.tedu.crm.notice.pojo.entity.Notice;
import cn.tedu.crm.notice_user.mapper.NoticeUserMapper;
import cn.tedu.crm.notice_user.pojo.entity.NoticeUser;
import cn.tedu.crm.notice.pojo.vo.NoticeVO;
import cn.tedu.crm.notice.service.NoticeService;
import cn.tedu.crm.operatelog.loggable.Loggable;
import cn.tedu.crm.operatelog.loggable.OperationModule;
import cn.tedu.crm.operatelog.loggable.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    NoticeMapper noticeMapper;
    @Autowired
    NoticeUserMapper noticeUserMapper;
    @Autowired
    UserMapper userMapper;


    @Transactional
    @Override
    @Loggable(module = OperationModule.SYSTEM_NOTICE,type = OperationType.INSERT)
    public void saveNotice(NoticeSaveParam noticeSaveParam) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeSaveParam,notice);
        log.debug("新增公告-service:入参{}",notice);
        notice.setReleaseTime(new Date());
        // 获取当前登录用户姓名
        notice.setPublisher(StpUtil.getSession().getString("username"));
        // 获取当前登录用户id
        notice.setUserId(StpUtil.getLoginIdAsLong());
        noticeMapper.insert(notice);

        // 发送消息给指定的用户
        Long noticeId = notice.getId();
        log.debug("公告id:{}", noticeId);
         List<Long> userIds;
        if ("all".equals(noticeSaveParam.getReceiverType())) {
            // 查询出所有的用户id
            userIds = userMapper.selectUserIds();
            log.debug("查询出所有的用户id:{}", userIds);
        } else {
            userIds = noticeSaveParam.getSelectedUsers();
        }

        if (userIds != null && userIds.size() > 0) {
            for (Long userId : userIds) {
                NoticeUser noticeUser = new NoticeUser();
                noticeUser.setNoticeId(noticeId);
                noticeUser.setUserId(userId);
                noticeUser.setStatus(10); // 10表示未读
                noticeUserMapper.insert(noticeUser);
            }
        }
    }
    @Override

    public List<NoticeVO> loadNotice(NoticeQuery noticeQuery) {
        log.debug("查询当前用户的公告列表-service:入参{}",noticeQuery);
        noticeQuery.setUserId(StpUtil.getLoginIdAsLong());
        List<NoticeVO> list = noticeMapper.selectByUserId(noticeQuery);
        log.debug("查询当前用户的公告列表-service:出参{}",list);
        return list;
    }

    @Override
    public List<NoticeVO> loadNoticeByIdAndUserId(Long id) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.debug("根据id和用户id查询公告-service:id:{},用户id:{}",id,userId);
        List<NoticeVO> noticeVOList = noticeMapper.selectByIdAndUserId(id,userId);
        log.debug("根据id和用户id查询公告-service:出参{}",noticeVOList);
        if(noticeVOList!=null && noticeVOList.size()>0){
            //修改状态为已读
            NoticeUser noticeUser = new NoticeUser();
            noticeUser.setNoticeId(id);
            noticeUser.setStatus(20);//20为已读
            noticeUserMapper.updateByNoticeId(noticeUser);
            log.debug("修改状态为已读-service:入参{}",noticeUser);
        }
        return noticeVOList;
    }
    @Transactional
    @Override
    @Loggable(module = OperationModule.SYSTEM_NOTICE,type = OperationType.DELETE)
    public void deleteNoticeById(Long id) {
        log.debug("删除公告-service:入参:{}",id);
        noticeMapper.deleteById(id);
        noticeUserMapper.deleteByNoticeId(id);

    }

    @Override
    public List<NoticeVO> selectByStatus(NoticeQuery noticeQuery) {
        log.debug("查询未读公告-service:入参:{}",noticeQuery);
        noticeQuery.setUserId(StpUtil.getLoginIdAsLong());
        noticeQuery.setStatus(10);
        log.debug("查询未读公告-service:状态入参:{}",noticeQuery.getStatus());
        List<NoticeVO> list = noticeMapper.selectByStatus(noticeQuery);
        log.debug("查询未读公告-service:出参:{}",list);
        return list;
    }

    @Override
    public Long selectNoticeCount(NoticeQuery noticeQuery) {
        noticeQuery.setUserId(StpUtil.getLoginIdAsLong());
        noticeQuery.setStatus(10);
        Long rows = noticeMapper.selectNoticeCount(noticeQuery);
        log.debug("查询未读公告数量-service:出参:数量{},参数:{}",rows,noticeQuery);
        return rows;
    }

}
